<?php
//版权所有(C) 2014 www.ilinei.com

namespace admin;

//模板解析器
class _ilinei{
    //获取模板页面，前台缓存
    public function load($theme){
        //加载模板页面缓存
        $pages = cache_read('pages');

        if(empty($pages)){
            $info_xml = (array)simplexml_load_file(ROOTPATH."/tpl/{$theme}/_info.xml");
            $_info_xml_pages = (array)$info_xml['pages'];

            if(is_array($_info_xml_pages['page'])){
                foreach($_info_xml_pages['page'] as $key => $page){
                    $page = (array)$page;

                    //取出当前结点，指定类型为页面
                    $page = $page['@attributes'];
                    $page['type'] = 'page';

                    $pages[$page['file']] = $page;
                }
            }else{
                $page = (array)$_info_xml_pages['page'];

                //取出当前结点，指定类型为页面
                $page = $page['@attributes'];
                $page['type'] = 'page';

                $pages[$page['file']] = $page;
            }

            //写入缓存
            cache_write('pages', $pages);
        }

        return $pages;
    }

    //获取当前模板的页面+块，后台编辑暂不缓存了。
    public function fetch($path){
        global $setting;

        //模板路径指定文件夹为page
        if($path != '/page') return array();

        $themes = explode('/', $setting['SiteTheme']);

        //如果当前模板无描述文件，拜拜吧
        if(!is_file(ROOTPATH."/tpl/{$themes[1]}/_info.xml")) return array();

        $pages = array();
        $info_xml = (array)simplexml_load_file(ROOTPATH."/tpl/{$themes[1]}/_info.xml");
        $_info_xml_pages = (array)$info_xml['pages'];

        if(is_array($_info_xml_pages['page'])){
            foreach($_info_xml_pages['page'] as $key => $page){
                $page = (array)$page;

                //取出当前结点，指定类型为页面
                $page = $page['@attributes'];
                $page['type'] = 'page';

                $pages[$page['file']] = $page;
            }
        }else{
            $page = (array)$_info_xml_pages['page'];

            //取出当前结点，指定类型为页面
            $page = $page['@attributes'];
            $page['type'] = 'page';

            $pages[$page['file']] = $page;
        }

        if(is_array($_info_xml_pages['block'])){
            foreach($_info_xml_pages['block'] as $key => $page){
                $page = (array)$page;

                //取出当前结点，指定类型为块
                $page = $page['@attributes'];
                $page['type'] = 'block';

                $pages[$page['file']] = $page;
            }
        }else{
            $page = (array)$_info_xml_pages['block'];

            //取出当前结点，指定类型为块
            $page = $page['@attributes'];
            $page['type'] = 'block';

            $pages[$page['file']] = $page;
        }

        return $pages;
    }

    //取出页面+块头部描述
    public function desc($file){
        $lines = get_file_lines($file, 2);

        if(substr($lines[0], 0, 10) == '<!--{@page') return substr(str_replace('}-->', '', $lines[0]), 10);
        elseif(substr($lines[0], 0, 11) == '<!--{@block') return substr(str_replace('}-->', '', $lines[0]), 11);

        return '';
    }

    //添加页面
    public function append($pages, $page){
        global $setting;

        $info_xml = (array)simplexml_load_file(ROOTPATH."/{$setting[SiteTheme]}/_info.xml");

        $info_xml_content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<theme>
	<application>{$info_xml[application]}</application>
	<name>{$info_xml[name]}</name>
	<version>{$info_xml[version]}</version>
	<author>{$info_xml[author]}</author>
	<thumb>{$info_xml[thumb]}</thumb>
	<pages>";

        foreach($pages as $key => $item){
            if(empty($item['file'])) continue;

            $info_xml_content .= "
        <{$item[type]} file=\"{$item[file]}\" name=\"{$item[name]}\"/>";
        }

        $info_xml_content .= "
        <{$page[type]} file=\"{$page[file]}\" name=\"{$page[name]}\"/>
    </pages>
</theme>";

        //写入_info.xml文件
        file_put_contents(ROOTPATH."/{$setting[SiteTheme]}/_info.xml", $info_xml_content);

        //删除缓存
        cache_delete('pages');
    }

    //删除页面
    public function delete($pages, $page){
        global $setting;

        $info_xml = (array)simplexml_load_file(ROOTPATH."/{$setting[SiteTheme]}/_info.xml");

        $info_xml_content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<theme>
	<application>{$info_xml[application]}</application>
	<name>{$info_xml[name]}</name>
	<version>{$info_xml[version]}</version>
	<author>{$info_xml[author]}</author>
	<thumb>{$info_xml[thumb]}</thumb>
	<pages>";

        foreach($pages as $key => $item){
            if(empty($item['file']) || $item['file'] == $page) continue;

            $info_xml_content .= "
        <{$item[type]} file=\"{$item[file]}\" name=\"{$item[name]}\"/>";
        }

        $info_xml_content .= "
    </pages>
</theme>";

        //写入_info.xml文件
        file_put_contents(ROOTPATH."/{$setting[SiteTheme]}/_info.xml", $info_xml_content);

        //删除缓存
        cache_delete('pages');
    }
}